<template>
  <div>
    <Mytable :arr="list">
      <template #header>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <template #body="{ row: obj, ind: index }">
        <td>{{ obj.id }}</td>
        <td>{{ obj.goods_name }}</td>
        <td>{{ obj.goods_price }}</td>
        <td>
          <input
            type="text"
            v-if="obj.inputVisible"
            v-model="obj.inputValue"
            v-focus
            @blur="blurfn(obj)"
            @keyup.enter="down(obj)"
            @keyup.esc="esc(obj)"
          />
          <button
            v-else
            @click="add(obj)"
            class="btn btn-primary btn-sm add-tag"
          >
            +Tag
          </button>
          <span
            v-for="(tag, index) in obj.tags"
            :key="index"
            class="badge badge-warning"
            >{{ tag }}</span
          >
        </td>
        <td>
          <button @click="del(index)" class="btn btn-danger btn-sm">
            删除
          </button>
        </td>
      </template>
    </Mytable>
  </div>
</template>

<script>
import axios from "axios";
import Mytable from "../components/mytable";
axios.defaults.baseURL = "https://www.escook.cn";
export default {
  methods: {
    del(index) {
      this.list.splice(index, 1);
    },
    add(obj) {
      obj.inputVisible = true;
    },
    blurfn(obj) {
      obj.inputVisible = false;
    },
    down(obj) {
      obj.tags.push(obj.inputValue);
    },
    esc(obj) {
      obj.inputValue = "";
    },
  },
  directives: {
    focus: {
      inserted(e) {
        e.focus();
      },
    },
  },
  data() {
    return {
      list: [],
    };
  },
  components: { Mytable },
  //   name: "MyTable",
  async created() {
    const res = await axios({
      url: "/api/goods",
    });
    console.log(res);
    this.list = res.data.data;
  },
};
</script>

<style>
.badge {
  margin-right: 5px;
}
</style>